﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Website.Models;

namespace Website.Controllers
{
    public class PageController : Controller
    {
        [Http(Allow = "DELETE")]
        public ActionResult Delete(string title)
        {
            WikiPage page = WikiPage.Query(title);
            if (page.IsAvailable)
            {
                WikiPage.Remove(title);
                return new NoContentActionResult();
            }
            else
            {
                return new NotFoundActionResult();
            }
        }

        [Http(Allow = "POST")]
        public ActionResult Post()
        {
            if (Request.IsSupportedMediaType("application/x-www-form-urlencoded"))
            {
                WikiPage page = WikiPage.Query(Request.Form["title"]);
                if (page.IsAvailable)
                {
                    page = WikiPage.Edit(Request.Form["title"], Request.Form["body"]);
                }
                else
                {
                    page = WikiPage.Edit(Request.Form["title"], Request.Form["body"]);
                }

                return new SeeOtherActionResult("/page/" + page.Title);
            }
            else
            {
                return new UnsupportedMediaTypeActionResult();
            }
        }

        [Http(Allow = "PUT")]
        public ActionResult Put(string title)
        {
            WikiPage page = WikiPage.Query(title);
            if (Request.IsSupportedMediaType("text/plain"))
            {
                if (page.IsAvailable)
                {
                    WikiPage.Edit(title, Request.InputStream.ReadString());
                    return new NoContentActionResult();
                }
                else
                {
                    WikiPage.Edit(title, Request.InputStream.ReadString());
                    return new CreatedActionResult("/page/" + title);
                }
            }
            else
            {
                return new UnsupportedMediaTypeActionResult();
            }
        }

        [Http(Allow = "GET, HEAD, OPTIONS")]
        public ActionResult ContentNegotiation(string title)
        {
            if (this.Request.Accepts("application", "atom+xml", false))
            {
                return new SeeOtherActionResult(String.Concat("/page/", title, ".atom"));
            }
            else if (this.Request.Accepts("text", "plain", false))
            {
                return new SeeOtherActionResult(String.Concat("/page/", title, ".txt"));
            }
            else
            {
                return new SeeOtherActionResult(String.Concat("/page/", title, ".html"));
            }
        }

        [Http(Allow = "GET, HEAD, OPTIONS", ContentType = "application/atom+xml", ContentMD5 = true)]
        public ActionResult AtomRepresentation(string title)
        {
            return View(WikiPage.Query(title));
        }

        [Http(Allow = "GET, HEAD, OPTIONS", ContentType = "text/html", ContentMD5 = true)]
        public ActionResult HtmlRepresentation(string title)
        {
            return View(WikiPage.Query(title));
        }

        [Http(Allow = "GET, HEAD, OPTIONS", ContentType = "text/plain", ContentMD5 = true)]
        public ActionResult TextRepresentation(string title)
        {
            return View(WikiPage.Query(title));
        }
    }
}